package com.guxingke.typecho4j.widget.handle.option;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.Option;
import com.guxingke.typecho4j.route.DispatcherStrategy;
import com.guxingke.typecho4j.route.Route;
import com.guxingke.typecho4j.route.Routes;
import com.guxingke.typecho4j.widget.WidgetHandle;
import com.guxingke.typecho4j.widget.layout.LayoutWidget;
import com.guxingke.typecho4j.widget.layout.back.option.GeneralOptionLayoutWidget;
import com.guxingke.typecho4j.widget.provider.OptionBaseWidget;
import com.jfinal.core.Controller;

/**
 * 更新编辑配置的处理组件
 * 
 * @author guxingke
 *
 */
public class UpdateGeneralOptionHandleWidget extends OptionBaseWidget implements
		WidgetHandle {
	@Override
	public void handle(Controller controller) throws Typecho4jException {
		String title = controller.getPara("title");
		String siteUrl = controller.getPara("siteUrl");
		String description = controller.getPara("description");
		String keywords = controller.getPara("keywords");

		setValue(new Option().set("name", "title").set("value", title));
		setValue(new Option().set("name", "siteUrl").set("value",
				siteUrl));
		setValue(new Option().set("name", "description").set("value",
				description));
		setValue(new Option().set("name", "keywords").set("value",
				keywords));

		// 刷新Provider的状态
		LayoutWidget.refreshLayout(GeneralOptionLayoutWidget.class);
	}

	@Override
	public void configRoute(Routes routes) {
		Route updateEditOptionRoute = new Route(
				"updateGenerralOptions", this.getClass(),
				"/admin/options-general",
				DispatcherStrategy.REDIRECT);
		routes.addRoute(updateEditOptionRoute);
	}
}
